using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;

using SBPweb.Contracts;

namespace SBPweb.Presentation.Windows.Forms.Host.Services
{
    public class EasterEggHandlerService : IEasterEggHandlerService
    {
        private Dictionary<string, EggCallBackDelegate> mDictionary;
        private bool mWatching = false;
        private string mBuffer = string.Empty;
        private Timer mTimer = new Timer();

        internal EasterEggHandlerService(Form host)
        {
            mDictionary = new Dictionary<string, EggCallBackDelegate>();

            mTimer.Tick += new EventHandler(mTimer_Tick);
            mTimer.Interval = 600;

            host.KeyUp -= new KeyEventHandler(host_KeyUp);
            host.KeyUp += new KeyEventHandler(host_KeyUp);

            host.KeyPreview = true;
        }

        public IEasterEgg RegisterEasterEgg(string keykode, EggCallBackDelegate callBackDelegate)
        {
            mDictionary.Add(keykode, callBackDelegate);

            EasterEgg ee = new EasterEgg(keykode, callBackDelegate);

            return ee;
        }

        public void UnRegisterEasterEgg(string keykode)
        {
            mDictionary.Remove(keykode);
        }

        public void UnRegisterEasterEgg(IEasterEgg egg)
        {
            mDictionary.Remove(egg.Keykode);
        }

        public List<IEasterEgg> GetAllEggs()
        {
            List<IEasterEgg> eggs = new List<IEasterEgg>();

            foreach (KeyValuePair<string, EggCallBackDelegate> egg in mDictionary)
            {
                eggs.Add(new EasterEgg(egg.Key, egg.Value));
            }

            return eggs;
        }

        public string Name
        {
            get 
            { 
                return "SBPweb.Presentation.Windows.Forms.Host.Services.EasterEggHandlerService";
            }
        }

        public SBPweb.Modules.Modularity.IModule Module
        {
            get 
            {
                return null;
            }
        }

        public int Interval
        {
            get
            {
                return mTimer.Interval;
            }
            set
            {
                mTimer.Interval = value;
            }
        }

        private void mTimer_Tick(object sender, EventArgs e)
        {
            mTimer.Stop();
            mWatching = false;
			bool foundEgg = false;

            foreach (KeyValuePair<string, EggCallBackDelegate> egg in mDictionary)
            {
                if (egg.Key.ToUpper() == mBuffer)
                {
                    //MessageBox.Show("Keykode " + mBuffer + " is activated.", "God mode");
					if (Properties.Settings.Default.PlaySpecialSounds)
					{
						System.Media.SystemSounds.Asterisk.Play();
					}
					egg.Value.DynamicInvoke();
					foundEgg = true;
                }
            }
			if (!foundEgg && Properties.Settings.Default.PlaySpecialSounds)
			{
				System.Media.SystemSounds.Beep.Play();
			}
        }

        private void host_KeyUp(object sender, KeyEventArgs e)
        {
            if (mWatching)
            {
                mTimer.Stop();
                mTimer.Start();
                if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z)
                    mBuffer = mBuffer + e.KeyCode.ToString();
            }
            else
                if (e.Alt && e.KeyCode == Keys.F12)
                {
                    e.Handled = true;
                    mBuffer = string.Empty;
                    mWatching = true;
                    mTimer.Start();
					if (Properties.Settings.Default.PlaySpecialSounds)
					{
						System.Media.SystemSounds.Exclamation.Play();
					}
                }
        }
    }

    public class EasterEgg : IEasterEgg
    {
        private string mKeykode;
        private EggCallBackDelegate mCallBackDelegate;

        internal EasterEgg(string keykode, EggCallBackDelegate callBackDelegate)
        {
            mKeykode = keykode;
            mCallBackDelegate = callBackDelegate;
        }

        public string Keykode
        {
            get 
            {
                return mKeykode;
            }
        }

        public EggCallBackDelegate CallBackDelegate
        {
            get 
            {
                return mCallBackDelegate;
            }
        }

    }
}
